package com.qlylkj.lbk.ui.cochain

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.forest.core.etx.startKtxActivity
import com.forest.core.etx.toast
import com.qlylkj.lbk.R
import com.qlylkj.lbk.base.BaseVMActivity
import com.qlylkj.lbk.model.bean.CoChainInfo
import com.qlylkj.lbk.ui.company.CompanyInfoActivity
import kotlinx.android.synthetic.main.activity_co_chain_info.*
import kotlinx.android.synthetic.main.layout_toolbar.*

class CoChainInfoActivity: BaseVMActivity<CoChainViewModel>() {

    private val coChainInfoAdapter by lazy { CoChainInfoAdapter() }

    override fun providerVMClass() = CoChainViewModel::class.java

    override fun setLayoutId() = R.layout.activity_co_chain_info

    override fun initView() {
        toolbar.setNavigationOnClickListener {finish()}
        toolbarTitle.text = "最新上链信息"
        coChainInfoRefreshLayout.apply {
            setOnRefreshListener {
                getCoChainInfoList(true)
            }
            setOnLoadMoreListener {
                getCoChainInfoList(false)
            }
        }
        coChainInfoRecyclerView.apply {
            layoutManager = LinearLayoutManager(this@CoChainInfoActivity)
            adapter = coChainInfoAdapter.apply {
                setOnItemClickListener { adapter, _, position ->
                    val coChainInfo = adapter.getItem(position)  as? CoChainInfo
                    coChainInfo?.apply {
                        consumers.apply {
                            startKtxActivity<CompanyInfoActivity>(values = mutableListOf(
                                CompanyInfoActivity.COMPANY_ID to id,
                                CompanyInfoActivity.COMPANY_NAME to companyName
                            ))
                        }
                    }
                }
            }
        }
    }

    override fun initData() {
        getCoChainInfoList(true)
    }

    override fun startObserve() {
        super.startObserve()
        mViewModel.apply {
            uiState.observe(this@CoChainInfoActivity, Observer { uiModel ->
                uiModel?.run {
                    coChainInfoRefreshLayout.finishRefresh()
                    coChainInfoRefreshLayout.finishLoadMore()
                    if(coChainInfoList.isNotEmpty()){
                        if(isRefresh){
                            coChainInfoRefreshLayout.resetNoMoreData()
                            coChainInfoAdapter.replaceData(coChainInfoList)
                        }else{
                            coChainInfoAdapter.addData(coChainInfoList)
                        }
                    }else{
                        coChainInfoRefreshLayout.finishLoadMoreWithNoMoreData()
                        coChainInfoRefreshLayout.setEnableFooterFollowWhenNoMoreData(true)
                    }
                    showError?.let {
                        toast(it)
                    }
                }
            })
        }
    }

    private fun getCoChainInfoList(isRefresh: Boolean){
        mViewModel.getCoChainInfoList("",isRefresh)
    }
}